# coding=utf-8
import datetime
import json

from src.db.flaskdb import db


class BaseModel(db.Model):
    """base database implement model"""

    # abstract class
    __abstract__ = True

    def to_dict(self):
        """convert model object to json object"""
        obj_dict = self.__dict__.copy()
        if "activity_status" in obj_dict.keys():
            cur_date = datetime.datetime.now().date()
            if obj_dict["start_time"] <= cur_date <= obj_dict["end_time"]:
                obj_dict["activity_status"] = "进行中"
            else:
                obj_dict["activity_status"] = "未开始"
        if '_sa_instance_state' in obj_dict.keys():
            del obj_dict['_sa_instance_state']
        if 'start_time' in obj_dict.keys():
            val = obj_dict["start_time"]
            if "datetime" in str(type(val)):
                obj_dict["start_time"] = obj_dict["start_time"].strftime("%Y-%m-%d")
        if 'new_publish_time' in obj_dict.keys():
            val = obj_dict["new_publish_time"]
            if "datetime" in str(type(val)):
                obj_dict["new_publish_time"] = obj_dict["new_publish_time"].strftime("%Y-%m-%d")
        if 'old_publish_time' in obj_dict.keys():
            val = obj_dict["old_publish_time"]
            if "datetime" in str(type(val)):
                obj_dict["old_publish_time"] = obj_dict["old_publish_time"].strftime("%Y-%m-%d")
        if 'effect_start' in obj_dict.keys():
            val = obj_dict["effect_start"]
            if "datetime" in str(type(val)):
                obj_dict["effect_start"] = obj_dict["effect_start"].strftime("%Y-%m-%d")
        if 'effect_end' in obj_dict.keys():
            val = obj_dict["effect_end"]
            if "datetime" in str(type(val)):
                obj_dict["effect_end"] = obj_dict["effect_end"].strftime("%Y-%m-%d")
        if 'end_time' in obj_dict.keys():
            val = obj_dict["end_time"]
            if "datetime" in str(type(val)):
                obj_dict["end_time"] = obj_dict["end_time"].strftime("%Y-%m-%d")
        if 'create_date' in obj_dict.keys():
            val = obj_dict["create_date"]
            if "datetime" in str(type(val)):
                obj_dict["create_date"] = obj_dict["create_date"].strftime("%Y-%m-%d %H:%M:%S")
        if 'is_finish' in obj_dict.keys():
            val = obj_dict["is_finish"]
            if val == 0:
                obj_dict["is_finish"] = "未启动"
            if val == 1:
                obj_dict["is_finish"] = "执行中"
            if val == 2:
                obj_dict["is_finish"] = "执行完毕"
            if val == 3:
                obj_dict["is_finish"] = "执行失败"
        if "remark" in obj_dict.keys():
            val = obj_dict["remark"]
            try:
                val = json.loads(val)
                val = str(val)
            except Exception as e:
                val = str(val)
        if "plan_type" in obj_dict.keys():
            val = obj_dict["plan_type"]
            obj_dict["plan_type_code"] = val
            if str(val) == "0":
                obj_dict["plan_type"] = "Multiple Max 多型号最大化利用"
            if str(val) == "1":
                obj_dict["plan_type"] = "Single Max 单型号最大化利用"
            if str(val) == "2":
                obj_dict["plan_type"] = "Dynamic Max 动态执行最大化利用"
        if 'product_level' in obj_dict.keys():
            val = obj_dict["product_level"]
            if val == 1:
                obj_dict["product_level"] = "一级-战略型号"
            if val == 2:
                obj_dict["product_level"] = "二级-重点型号"
            if val == 3:
                obj_dict["product_level"] = "三级-普通型号"
        if 'burst_end_time' in obj_dict.keys():
            val = obj_dict["burst_end_time"]
            if "datetime" in str(type(val)):
                obj_dict["burst_end_time"] = obj_dict["burst_end_time"].strftime("%Y-%m-%d")
            if val == "0000-00-00":
                obj_dict["burst_end_time"] = ""
        if 'ck' in obj_dict.keys():
            val = obj_dict["ck"]
            if "datetime" in str(type(val)):
                obj_dict["ck"] = obj_dict["ck"].strftime("%Y-%m-%d")
        if 'burst_start_time' in obj_dict.keys():
            val = obj_dict["burst_start_time"]
            if "datetime" in str(type(val)):
                obj_dict["burst_start_time"] = obj_dict["burst_start_time"].strftime("%Y-%m-%d")
            if val == "0000-00-00":
                obj_dict["burst_start_time"] = ""
        if 'org_type' in obj_dict.keys():
            val = obj_dict["org_type"]
            if val == 1:
                obj_dict["org_type"] = "一级组织"
            if val == 2:
                obj_dict["org_type"] = "二级组织"
            if val == 3:
                obj_dict["org_type"] = "三级组织"
        if 'org_id' in obj_dict.keys():
            val = obj_dict["org_id"]
            obj_dict["org_id"] = str(val)
        if 'ds' in obj_dict.keys():
            val = obj_dict["ds"]
            if "datetime" in str(type(val)):
                obj_dict["ds"] = obj_dict["ds"].strftime("%Y-%m-%d")
        if 'create_time' in obj_dict.keys():
            val = obj_dict["create_time"]
            if "datetime" in str(type(val)):
                obj_dict["create_time"] = obj_dict["create_time"].strftime("%Y-%m-%d")
        if 'dsm' in obj_dict.keys():
            val = obj_dict["dsm"]
            if "datetime" in str(type(val)):
                obj_dict["dsm"] = obj_dict["dsm"].strftime("%Y-%m")
        if 'order_date' in obj_dict.keys():
            val = obj_dict["order_date"]
            if "datetime" in str(type(val)):
                obj_dict["order_date"] = obj_dict["order_date"].strftime("%Y-%m-%d")
        if 'log_date' in obj_dict.keys():
            val = obj_dict["log_date"]
            if "datetime" in str(type(val)):
                obj_dict["log_date"] = obj_dict["log_date"].strftime("%Y-%m-%d %H:%M:%S")
        if 'log_msg' in obj_dict.keys():
            val = obj_dict["log_msg"]
            log_dic = json.loads(val)
            log_msg = log_dic.get("message")
            obj_dict["log_msg"] = log_msg
        return obj_dict

    def get_key_values(self):
        """convert model object to list"""
        obj_dict = self.__dict__
        if '_sa_instance_state' in obj_dict.keys():
            del obj_dict['_sa_instance_state']
        return obj_dict.keys(), obj_dict.values()


def GetString(length, primary_key=False, default=None):
    """get column string"""
    column = None
    if primary_key is True:
        column = db.Column(db.String(length), primary_key=primary_key)
    else:
        column = db.Column(db.String(length), primary_key=primary_key, default=default)
    return column


def GetInteger(primary_key=False, default=0, auto_increment=False):
    """get column integer"""
    column = None
    if primary_key is True:
        column = db.Column(db.Integer, primary_key=True, auto_increment=auto_increment)
    else:
        column = db.Column(db.Integer, default=default)
    return column


def GetDatetime(primary_key=False, default=None):
    """get column datetime"""
    column = db.Column(db.DateTime, primary_key=primary_key, default=default)
    return column


def GetNumber(precision=11, scale=2, primary_key=False, default=0):
    """get column number"""
    column = db.Column(db.Numeric(precision, scale), primary_key=primary_key, default=default)
    return column


def GetDate(primary_key=False, default=None):
    """get column date"""
    column = db.Column(db.Date, primary_key=primary_key, default=default)
    return column


def GetLong(primary_key=False, default=0):
    """get column Long"""
    column = db.Column(db.BigInteger, primary_key=primary_key, default=default)
    return column


def GetText(primary_key=False, default=""):
    """get column text"""
    column = db.Column(db.Text, primary_key=primary_key, default=default)
    return column
